export default {
  namespaced: true,
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false
      }
    ],
    checkAll: false
  },
  getters: {
    getTotal (state) {
      const checkedGoods = state.goodList.filter(item => item.checked)
      return checkedGoods.reduce((sum, item) => sum + item.price * item.num, 0)
    },
    isChecked (state) {
      state.checkAll = state.goodList.every(item => item.checked === true)
    }
  },
  mutations: {
    changeChecked (state, payload) {
      // console.log(payload)
      const obj = state.goodList.find(item => item.name === payload)
      obj.checked = !obj.checked
    },
    changeNum (state, payload) {
      // console.log(payload)
      const obj = state.goodList.find(item => item.name === payload.name)
      obj.num = payload.num
    },
    delItem (state, payload) {
      state.goodList = state.goodList.filter(item => item.name !== payload)
    },
    checkAll (state) {
      state.checkAll = !state.checkAll
      // state.goodList.forEach(item => item.checked = state.checkAll)
      state.goodList.forEach(item => { item.checked = state.checkAll })
    }
  }
}
